package com.pmoc.framework.core;

import com.pmoc.framework.core.task.CheckTask;
import com.pmoc.framework.core.task.WorkTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.hibernate.type.TimeType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * AppConfig
 *
 * @author humdeef
 */
@Configuration
@ComponentScan(basePackageClasses = AppConfig.class)
@Slf4j
public class AppConfig implements InitializingBean {

    @Autowired
    private WorkTask workTask;
    @Autowired
    private CheckTask checkTask;

    @Override
    public void afterPropertiesSet() throws Exception {
        ScheduledExecutorService scheduledExecutorService =
                new ScheduledThreadPoolExecutor(4, new BasicThreadFactory.Builder().namingPattern("scheduled task").build());
        log.info("5秒后开始执行消费程序  " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
        log.info("30分钟后开始执行校验程序 " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
        scheduledExecutorService.scheduleAtFixedRate(workTask, 5, 20, TimeUnit.SECONDS);
        scheduledExecutorService.scheduleAtFixedRate(checkTask, 30, 30, TimeUnit.MINUTES);
    }
}
